package weaver.interfaces.cese.duss.ceseutil;

import com.api.workflow.constant.RequestAuthenticationConstant;
import com.engine.workflow.biz.requestForm.FileBiz;
import weaver.conn.RecordSet;
import weaver.docs.docs.DocImageManager;
import weaver.docs.docs.DocUpload;
import weaver.file.FileUpload;
import weaver.general.Util;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.hrm.resource.ResourceComInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 上传附件并生成文档
 * */
public class UploadFile2Doc {

	public static Map<String,Object> upload(HttpServletRequest request, HttpServletResponse response){
		Map<String,Object> apidatas =  new HashMap<String,Object>();
	    try {
			response.setHeader("cache-control", "no-cache");
			response.setHeader("pragma", "no-cache");
			response.setHeader("expires", "Mon 1 Jan 1990 00:00:00 GMT");

			FileUpload fu = new FileUpload(request, "utf-8");
			int mainId = Util.getIntValue(fu.getParameter("mainId"),0);
			int subId = Util.getIntValue(fu.getParameter("subId"),0);
			int secId = Util.getIntValue(fu.getParameter("secId"),0);
			int userid = Util.getIntValue(fu.getParameter("userid"));
			int logintype = Util.getIntValue(fu.getParameter("logintype"));
			
			String category = Util.null2String(fu.getParameter("category"));
			if(!"".equals(category)){
				String[] arr  = category.split(",");
				if(arr.length==1){//如果目录就传了一层,默认只给到secId
					secId = Util.getIntValue(arr[0],0);
				}else{
					if(arr.length > 0 ) mainId = Util.getIntValue(arr[0],0);
					if(arr.length > 1 ) subId = Util.getIntValue(arr[1],0);
					if(arr.length > 2 ) secId = Util.getIntValue(arr[2],0);
				}
			}
			
			User user = null;
			if (userid < 1) {
			    user = HrmUserVarify.getUser(request, response);
			} else {
			    RecordSet rs = new RecordSet();
			    user=new User();
			    user.setUid(userid);
			    if (logintype == 2) {
			        rs.execute("CRM_CustomerInfo_SelectByID", "" + userid);
			        if (rs.next()) {
			            user.setLoginid(rs.getString("portalloginid"));
			            user.setFirstname(rs.getString("name"));
			            String languageidweaver = rs.getString("language");
			            user.setLanguage(Util.getIntValue(languageidweaver, 7));
			            user.setUserDepartment(Util.getIntValue(rs.getString("department"), 0));
			            user.setUserSubCompany1(Util.getIntValue(rs.getString("subcompanyid1"), 0));
			            user.setManagerid(rs.getString("manager"));
			            user.setCountryid(rs.getString("country"));
			            user.setEmail(rs.getString("email"));
			            user.setLogintype("2");
			            user.setSeclevel(rs.getString("seclevel"));
			        }
			    } else {
			        ResourceComInfo rci = new ResourceComInfo();
			        user.setLoginid(rci.getLoginID("" + userid));
			        user.setLogintype("1");
			        user.setFirstname(rci.getFirstname("" + userid));
			        user.setLastname(rci.getLastname("" + userid));
			        user.setSex(rci.getSexs("" + userid));
			        user.setLanguage(Util.getIntValue(rci.getSystemLanguage("" + userid), 7));
			        user.setTelephone(rci.getTelephone("" + userid));
			        user.setMobile(rci.getMobile("" + userid));
			        user.setEmail(rci.getEmail("" + userid));
			        user.setLocationid(rci.getLocationid("" + userid));
			        user.setResourcetype(rci.getResourcetype("" + userid));
			        user.setJobtitle(rci.getJobTitle("" + userid));
			        user.setJoblevel(rci.getJoblevel("" + userid));
			        user.setSeclevel(rci.getSeclevel("" + userid));
			        user.setUserDepartment(Util.getIntValue(rci.getDepartmentID("" + userid)));
			        user.setUserSubCompany1(Util.getIntValue(rci.getSubCompanyID("" + userid)));
			        user.setManagerid(rci.getManagerID("" + userid));
			        user.setAssistantid(rci.getAssistantID("" + userid));
			        user.setAccount(rci.getAccount("" + userid));
			    }
			}
			if (user == null) return apidatas;
			
			String[] filedata = new String[1];
			filedata[0] = "file";
			int[] returnarry = null;
			returnarry = DocUpload.uploadDocsToImgs(fu, user, filedata,mainId,subId,secId,"","");
			String tempvalue = "";
			if (returnarry != null) {
			    for (int i = 0; i < returnarry.length; i++) {
			        if (returnarry[i] != -1)
			            if (tempvalue.trim().equals(""))
			                tempvalue = String.valueOf(returnarry[i]);
			            else
			                tempvalue = tempvalue + "," + String.valueOf(returnarry[i]);
			    }
			}

			//获取上次的
			String listType = Util.null2String(request.getParameter("listType"),"list");
			int requestid = Util.getIntValue(Util.null2String(request.getParameter("requestid")), -1);
			int desrequestid = Util.getIntValue(Util.null2String(request.getParameter("desrequestid")), -1);
			int isprint = Util.getIntValue(Util.null2String(request.getParameter("isprint")), 0);
			int workflowid = Util.getIntValue(Util.null2String(request.getParameter("workflowid")), 0);
			String f_weaver_belongto_userid = Util.null2String(request.getParameter("f_weaver_belongto_userid"));
			String f_weaver_belongto_usertype = Util.null2String(request.getParameter("f_weaver_belongto_usertype"));
			String authStr = Util.null2String(request.getParameter( RequestAuthenticationConstant.AUTHORITY_STRING));
			String authSignatureStr = Util.null2String(request.getParameter(RequestAuthenticationConstant.AUTHORITY_SIGNATURESTRING));
			Map<String,Object> retobj = FileBiz.getFileDatas(tempvalue,listType,requestid,desrequestid,isprint,f_weaver_belongto_userid,f_weaver_belongto_usertype,true,false,authStr,authSignatureStr);
			if(retobj != null){
				Object _filedatas = retobj.get("filedatas");
				if(_filedatas != null){
					List<Map<String, String>> filedatas = (List<Map<String, String>>)_filedatas;
					if(filedatas.size() > 0){
						Map<String,String> filedata0 = filedatas.get(0);
						String docid = filedata0.get("fileid");
						String imagefileid = filedata0.get("imagefileid");
						DocImageManager dim = new DocImageManager();
						dim.resetParameter();
						dim.setVersionDetail("128095");
						//dim.setOperateuserid(user.getUID());
						dim.setDocid(Util.getIntValue(docid));
						dim.setImagefileid(imagefileid);
						//dim.updateDocImageOperaInfo();
						apidatas.put("data",filedata0);
					}
				}
			}
		} catch (RuntimeException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return apidatas;
	}
	
}
